package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetMealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;


/**
 * Created with IntelliJ IDEA.
 *
 * @Author: HuoXu
 * @Date: 2023/08/26/11:40
 * @Description: 快乐跳跳虎
 */
@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetMealMapper setMealMapper;

    /**
     * 添加购物车
     * @param shoppingCartDTO
     */
    @Override
    public void addCart(ShoppingCartDTO shoppingCartDTO) {
        //判断当前加入购物车的菜品是否已经在数据库中存在了
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        shoppingCart.setUserId(BaseContext.getCurrentId());
        List<ShoppingCart> list=shoppingCartMapper.selectCart(shoppingCart);
        //如果已经存在则直接进行更新操作在对应的数据number的基础上加一
        if(list !=null && list.size()>0){
            ShoppingCart cart = list.get(0);
            cart.setNumber(cart.getNumber()+1);
            //进行数据库的修改操作
            shoppingCartMapper.updateCart(cart);
        }else {
                //如果当前添加的菜品在数据库中不存在的话直接进行新增操作

                //首先进行判断本次加入购物车的数据时套餐还是单独的菜品
                Long dishId = shoppingCartDTO.getDishId();
                Long setmealId = shoppingCartDTO.getSetmealId();
                if(dishId != null){
                    //确定本次添加的是菜品不是套餐，查询菜品的数据
                    Dish dish = dishMapper.queryById(dishId);
                    shoppingCart.setName(dish.getName());
                    shoppingCart.setImage(dish.getImage());
                    shoppingCart.setAmount(dish.getPrice());
                    shoppingCart.setNumber(1);
                    shoppingCart.setCreateTime(LocalDateTime.now());
                }else {
                    SetmealVO setmealVO = setMealMapper.queryById(setmealId);
                    shoppingCart.setName(setmealVO.getName());
                    shoppingCart.setImage(setmealVO.getImage());
                    shoppingCart.setAmount(setmealVO.getPrice());
                    shoppingCart.setNumber(1);
                    shoppingCart.setCreateTime(LocalDateTime.now());
                }
            shoppingCartMapper.addCart(shoppingCart);
        }
    }

    @Override
    public List<ShoppingCart> queryShoppingCart() {
        return shoppingCartMapper.queryShoppingCart(BaseContext.getCurrentId());
    }

    /**
     * 购物车物品减少
     * @param shoppingCartDTO
     */
    @Override
    public void shoppingCartSub(ShoppingCartDTO shoppingCartDTO) {
        //判断减少的是单独的菜品还是套餐
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        shoppingCart.setUserId(BaseContext.getCurrentId());
        //如果是菜品则进行更新
        List<ShoppingCart> list=shoppingCartMapper.selectCart(shoppingCart);
        ShoppingCart cart = list.get(0);
        if(cart.getNumber()-1==0){
            shoppingCartMapper.deleteCart(shoppingCart);
        }else {
            shoppingCart.setNumber(cart.getNumber()-1);
            shoppingCartMapper.shoppingCartSub(shoppingCart);
        }
    }

    /**
     * 清空购物车
     */
    @Override
    public void shoppingCartClean() {
        shoppingCartMapper.shoppingCartClean(BaseContext.getCurrentId());
    }
}
